/**
 * Panel représentant l'écran 
 * 
 * @author Marie-Hélène Goddet, Damien Finck, Cédric Schaller
 * 
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import utils.Resources;

@SuppressWarnings("serial")
public class JScreen extends JPanel {
	
	private ActionListener actionListener;
	
	protected JPanel jp_haut;
		protected JButton jb_home;
		protected JPanel jp_to;protected JButton jb_to;
		protected JLabel jl_position;
		protected JButton jb_send;
	protected JScrollPane jsp_ecran;protected JTextArea jta_ecran;
	protected JPanel jp_bas;
		protected JButton jb_left;
		protected JPanel jp_propositions;
			protected JMot[] jl_choix;
		protected JButton jb_right;
	
	public final static int NB_CHOIX=7;
	
	/**
	 * Constructeur
	 */
	public JScreen() {
		/** Evénements **/
		ActionListener action = new Event_JPScreen(this);
		/** Création des éléments     **/
		this.jp_haut = new JPanel(new BorderLayout());
			this.jb_home = new JButton("Accueil",Resources.getImageIcon("images/home.png",getClass()));
			this.jp_to = new JPanel();this.jb_to = new JButton("Destinataires");
			this.jl_position = new JLabel("(x=0;y=0)");
			this.jb_send = new JButton("Envoyer",Resources.getImageIcon("images/mail.png",getClass()));
		//this.jp_ecran = new JPanel(new BorderLayout());
			this.jta_ecran = new JTextArea();
			this.jsp_ecran = new JScrollPane(this.jta_ecran);
		this.jp_bas = new JPanel(new BorderLayout());
			this.jb_left = new JButton(Resources.getImageIcon("images/left_gris.png", getClass()));
			
			this.jp_propositions = new JPanel(new FlowLayout(FlowLayout.CENTER));//new GridLayout(1,NB_CHOIX));
				
				this.jl_choix = new JMot[NB_CHOIX];
				for(int i=0;i<NB_CHOIX;i++) this.jl_choix[i] = new JMot("",SwingConstants.CENTER);
				
			this.jb_right = new JButton(Resources.getImageIcon("images/right_gris.png", getClass()));
		/** Paramétrage des éléments  **/
		this.setLayout(new BorderLayout());
		this.jta_ecran.setRows(9);				// 9 lignes dans l'écran
		this.jta_ecran.setLineWrap(true);		// aller a la ligne automatiquement
		this.jta_ecran.setWrapStyleWord(true);	//ne pas couper les mots
		this.jsp_ecran.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		this.jsp_ecran.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		this.jb_left.setPreferredSize(new Dimension(32,16));
		this.jb_right.setPreferredSize(new Dimension(32,16));
		this.jb_left.setEnabled(false);
		this.jb_right.setEnabled(false);
		this.jl_position.setVisible(false);
		for(int i=0;i<NB_CHOIX;i++) this.jl_choix[i].setVisible(false);
		/** Action sur les éléments   **/
		this.jb_home.addActionListener(action);
		this.jb_to.addActionListener(action);
		this.jb_send.addActionListener(action);
		/** Montage des éléments      **/
		this.add(this.jp_haut,BorderLayout.NORTH);
			this.jp_haut.add(this.jb_home,BorderLayout.WEST);
			this.jp_haut.add(this.jp_to,BorderLayout.CENTER);
				this.jp_to.add(this.jb_to);
			//this.jp_haut.add(this.jl_position);
			this.jp_haut.add(this.jb_send,BorderLayout.EAST);
		this.add(this.jsp_ecran,BorderLayout.CENTER);
			//this.jsp_ecran.add(this.jta_ecran,BorderLayout.CENTER);
		this.add(this.jp_bas,BorderLayout.SOUTH);
			this.jp_bas.add(this.jb_left,BorderLayout.WEST);
			this.jp_bas.add(this.jp_propositions,BorderLayout.CENTER);
			for(int i=0;i<NB_CHOIX;i++) this.jp_propositions.add(this.jl_choix[i]);
			this.jp_bas.add(this.jb_right,BorderLayout.EAST);
	}
	public void addActionListener(ActionListener actionListener) {
		this.actionListener = actionListener;
	}
	public ActionListener getActionListener() {
		return actionListener;
	}
}
class Event_JPScreen implements ActionListener {
	JScreen jp;
	/**
	 * Constructeur
	 * @param jp l'écran
	 */
	public Event_JPScreen(JScreen jp) {
		this.jp=jp;
	}
	/**
	 * Appelé lors de l'appui sur un bouton en haut de l'interface
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object obj = arg0.getSource();
		if (obj instanceof JButton) {
			JButton jb = (JButton)obj;
			if (jb==jp.jb_home) {
				ActionEvent e = new ActionEvent(jp,1001,"JPScreen_home");
				jp.getActionListener().actionPerformed(e);
			} else if (jb==jp.jb_to) {
				ActionEvent e = new ActionEvent(jp,1001,"JPScreen_contact");
				jp.getActionListener().actionPerformed(e);
			} else if (jb==jp.jb_send) {
				ActionEvent e = new ActionEvent(jp,1001,"JPScreen_send");
				jp.getActionListener().actionPerformed(e);
			}
		}
	}
}